package com.kzd.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.callback.KZD_OnSmallListener;
import com.kzd.sdk.core.KZD_ApiConstance;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_LoginSmall;
import com.kzd.sdk.entity.KZD_SmallGroup;
import com.kzd.sdk.ui.adapter.KZD_SmallAccountAdapter;
import com.kzd.sdk.ui.dialog.KZD_SmallAccountDialog;
import com.kzd.sdk.utils.AppUtils;

import java.util.List;

public class KZD_UserSmallActivity extends OwnerActivity {

    public static void open(Context context) {
        Intent intent = new Intent(context, KZD_UserSmallActivity.class);
        context.startActivity(intent);
    }


    private KZD_SmallAccountAdapter mSmallAccountAdapter;
    private KZD_SmallAccountDialog mSmallAccountDialog;

    //小号列表回调
    private final KZD_HttpCallback<List<KZD_SmallGroup>> httpListenerSmallList = new KZD_HttpCallback<List<KZD_SmallGroup>>() {
        @Override
        public void onSuccess(List<KZD_SmallGroup> data) {
            mSmallAccountAdapter.setNewInstance(data);
        }

        @Override
        public void onError(String message) {
            toast(message);
        }
    };

    //小号添加回调
    private final KZD_HttpCallback<String> httpListenerAdd = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            mSmallAccountDialog.cancel();
            KZD_ApiManager.httpSmallAccountList(httpListenerSmallList);
        }

        @Override
        public void onError(String message) {
            toast(message);
        }
    };

    //小号登录回调
    private final KZD_HttpCallback<KZD_LoginSmall> httpListenerLogin = new KZD_HttpCallback<KZD_LoginSmall>() {
        @Override
        public void onSuccess(KZD_LoginSmall data) {
            KZD_ApiManager.httpSmallAccountList(httpListenerSmallList);
            KZD_SdkController.getOnLoginListener().onLoginSwitch(KZD_ApiConstance.LOGIN_SMALL);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_user_small";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    @Override
    public void initView() {

        findViewById(AppUtils.getId((this),"ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });

        LinearLayout llUserSmallAdd = findViewById(AppUtils.getId((this), "llUserSmallAdd"));
        RecyclerView rvUserSmallBody = findViewById(AppUtils.getId((this), "rvUserSmallBody"));
        //实例化小号列表适配器
        mSmallAccountAdapter = new KZD_SmallAccountAdapter((this), (2));
        //实例化小号创建对话框
        mSmallAccountDialog = new KZD_SmallAccountDialog((this));

        rvUserSmallBody.setLayoutManager(new LinearLayoutManager((this)));
        rvUserSmallBody.setAdapter(mSmallAccountAdapter);

        //小号列表条目点击事件
        mSmallAccountAdapter.setOnItemClickListener(new KZD_OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                String smallUid = mSmallAccountAdapter.get(position).getUid();
                KZD_ApiManager.httpSmallAccountLogin(smallUid, httpListenerLogin);
            }
        });

        //添加小号确认点击事件
        mSmallAccountDialog.setOnClickListener(new KZD_OnSmallListener() {
            @Override
            public void onName(String name) {
                KZD_ApiManager.httpSmallAccountAdd(name, httpListenerAdd);
            }
        });

        //添加小号点击事件
        llUserSmallAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSmallAccountDialog.show();
            }
        });
    }

    @Override
    public void initData() {
        KZD_ApiManager.httpSmallAccountList(httpListenerSmallList);
    }
}
